package com.bignerdranch.android.ecar.PhotoPicker;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.bignerdranch.android.ecar.R;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import me.iwf.photopicker.PhotoPicker;
import me.iwf.photopicker.PhotoPreview;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by Administrator on 2016/8/1.
 */
public class PhotoActivity extends AppCompatActivity implements View.OnClickListener {
    private Button imags;
    ArrayList<String> selectedPhotos = new ArrayList<>();
    //    private RecyclerView recycler_view;
    RecyclerView recyclerView;
    PhotoAdapter photoAdapter;
    private Button choose;
    private Button upload;
    //参数类型
    private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
    //创建OkHttpClient实例
    private final OkHttpClient client = new OkHttpClient();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo);
        initView();

      /*  imags = (Button) findViewById(R.id.choose);
        imags.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                checkPermission();
                PhotoPicker.builder()
                        .setPhotoCount(9)
                        .setGridColumnCount(4)
                        .start(PhotoActivity.this);
            }
        });*/
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK &&
                (requestCode == PhotoPicker.REQUEST_CODE || requestCode == PhotoPreview.REQUEST_CODE)) {

            List<String> photos = null;
            if (data != null) {
                photos = data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
            }
            selectedPhotos.clear();

            if (photos != null) {

                selectedPhotos.addAll(photos);
            }
            photoAdapter.notifyDataSetChanged();
        }
    }

    private void initView() {
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        photoAdapter = new PhotoAdapter(this, selectedPhotos);

        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(4, OrientationHelper.VERTICAL));
        recyclerView.setAdapter(photoAdapter);
        choose = (Button) findViewById(R.id.choose);
        choose.setOnClickListener(this);
        upload = (Button)findViewById(R.id.upload);
        upload.setOnClickListener(this);

        recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                PhotoPreview.builder()
                        .setPhotos(selectedPhotos)
                        .setCurrentItem(position)
                        .start(PhotoActivity.this);
            }
        }));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.upload:
                String url = "http://172.16.14.100:8080/CarMarket/notes/notesAction_addNote.action";
                MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
                for (int i = 0; i <selectedPhotos.size() ; i++) {
                    Log.e("====",""+selectedPhotos.size());
                    File f=new File(selectedPhotos.get(i));
                    if (f!=null) {
                        builder.addFormDataPart("img", f.getName(), RequestBody.create(MEDIA_TYPE_PNG, f));
                    }
                }
                //添加其它信息
                builder.addFormDataPart("userId","52").addFormDataPart("content","et");
                MultipartBody requestBody = builder.build();
                //构建请求
                Request request = new Request.Builder()
                        .url(url)
                        .post(requestBody)//添加请求体
                        .build();

                client.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.e("_____","上传失败");
                    }
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        Log.e("上传照片成功：response = ", response.body().string());
                    }
                });



                break;

            case R.id.choose:
                PhotoPicker.builder()
                        .setPhotoCount(9)
                        .setGridColumnCount(4)
                        .start(PhotoActivity.this);
                break;
        }
    }
}
